package cn.kgc.crowdfundingadmin.service;

import cn.kgc.crowdfundingadmin.mapper.CertMapper;
import cn.kgc.crowdfundingadmin.pojo.Cert;
import cn.kgc.crowdfundingadmin.pojo.Role;
import cn.kgc.crowdfundingadmin.pojo.RoleExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("certMservice")
public class CertMserviceImpl implements  CertMservice{
    @Resource
    CertMapper certMapper;
    @Override
    public PageInfo<Cert> queryForKeywordWithPage(Integer pageNum, Integer pageSize, String keyword) {
        //进行分页的查询
        //1.开启分页功能
        PageHelper.startPage(pageNum,pageSize);
        //2.执行查询
        RoleExample example=new RoleExample();
        RoleExample.Criteria criteria = example.createCriteria();
        //跟据名字查询  模糊
        criteria.andNameLike("%"+keyword+"%");
        List<Cert> list = (List<Cert>) certMapper.selectByExample(example);
        PageInfo<Cert> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public void batchRemove(List<Integer> roleIdList) {
        RoleExample roleExample = new RoleExample();

        roleExample.createCriteria().andIdIn(roleIdList);

        certMapper.deleteByExample(roleExample);
    }

    @Override
    public List<Cert> getRoleListByIdList(List<Integer> roleIdList) {
        // 创建实体类Role对应的Example对象
        RoleExample roleExample = new RoleExample();

        // 在Example对象中封装查询条件
        roleExample.createCriteria().andIdIn(roleIdList);

        // 执行查询
        return certMapper.selectByExample(roleExample);
    }

    @Override
    public void updateRole(Cert role) {
        certMapper.updateByPrimaryKey(role);
    }

    @Override
    public void saveRole(String roleName) {
        certMapper.insert(new Cert(null, roleName));
    }


}
